<template>
  <div class="spu-preview" @click="handleClick">
    <div class="spu-image">
      <img :src="spu.img" :alt="spu.title" />
      <div v-if="spu.discount_price" class="discount-tag">
        {{ getDiscountText(spu) }}
      </div>
    </div>
    <div class="spu-info">
      <div class="spu-title">{{ spu.title }}</div>
      <div class="spu-price">
        <span class="current-price">¥{{ spu.discount_price || spu.price }}</span>
        <span v-if="spu.discount_price" class="original-price">¥{{ spu.price }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useCartStore } from '@/stores/cart'
import { showToast } from 'vant'

// 定义 props
const props = defineProps({
  spu: {
    type: Object,
    required: true
  }
})

// 定义 emits
const emit = defineEmits(['click'])

const cartStore = useCartStore()

// 获取折扣文本
const getDiscountText = (spu) => {
  if (spu.discount_price && spu.price) {
    const discount = Math.round((spu.discount_price / spu.price) * 100)
    return `${discount}折`
  }
  return ''
}

// 处理点击事件
const handleClick = () => {
  emit('click')
}
</script>

<style lang="scss" scoped>
.spu-preview {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: transform 0.2s ease;
  
  &:hover {
    transform: translateY(-2px);
  }
}

.spu-image {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 100%;
  
  img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  
  .discount-tag {
    position: absolute;
    top: 8px;
    right: 8px;
    background: #ff6b6b;
    color: #fff;
    font-size: 12px;
    padding: 2px 6px;
    border-radius: 4px;
  }
}

.spu-info {
  padding: 8px;
}

.spu-title {
  font-size: 14px;
  color: #333;
  line-height: 1.4;
  height: 40px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  margin-bottom: 5px;
}

.spu-price {
  display: flex;
  align-items: center;
  gap: 5px;
  
  .current-price {
    font-size: 16px;
    font-weight: bold;
    color: #ff6b6b;
  }
  
  .original-price {
    font-size: 12px;
    color: #999;
    text-decoration: line-through;
  }
}
</style> 